package com.lmk.controller;

import com.lmk.domain.Store;
import com.lmk.service.StoreService;
import com.lmk.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/store")
@RestController
public class StoreController {

    @Autowired
    private StoreService storeService;

    // 查询所有
    @GetMapping("/getAll")
    public List<Store> getAll(){
        return storeService.getAll();
    }

    // 查询单个
    @GetMapping("/getById/{id}")
    public Store getById(@PathVariable("id") Long id){
        return storeService.getById(id);
    }

    // 添加或修改
    @PostMapping("/addorupdate")
    public AjaxResult addorupdate( @RequestBody Store store){
        try {
            if(store.getId()==null){
                storeService.add(store);
            } else {
                storeService.update(store);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    // 删除
    @DeleteMapping("/del/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            storeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
}
